package com.csd.interceptor;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.csd.exception.DeleteException;
import com.csd.exception.GetException;
import com.csd.exception.UpdateException;
import com.csd.exception.SaveException;
import com.csd.helper.ActionResult;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

/**
 * 
 * Interceptor：异常拦截器
 * @author csd
 * @version 1.0
 * 
 * <br> 修订时间：2010-1-7
 * <br> 修改原因：创建文件
 * <br> 修改人：刘世彬
 * 
 * <br> 
 * 
 */
public class ExceptionInterceptor implements Interceptor {

	public void destroy() {
		// TODO Auto-generated method stub

	}

	public void init() {
		// TODO Auto-generated method stub

	}
	
	/* 
	 * 用于拦截Action/Service层的所有异常
	 */
	public String intercept(ActionInvocation invoker) throws Exception {
		try {
			String result = invoker.invoke();
			return result;
			//异常处理按种类分成四种异常，分别处理
		} catch (DeleteException exception) {
			handleDeleteException(exception);
		} catch (UpdateException exception) {
			handleUpdateException(exception);
		} catch (SaveException exception) {
			handleSaveException(exception);
		} catch (GetException exception) {
			handleGetException(exception);
		} catch (Exception exception) {
			handleUnexpectedException(exception);
		}
		return ActionResult.fail;
	}
	
	
	
	private void handleDeleteException(DeleteException exception) {
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("info", exception.getMessage());
		request.setAttribute("exception", exception.getCause());
		request.setAttribute("returnpage", "/right_well.jsp");

	}

	private void handleUpdateException(UpdateException exception) {
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("info", exception.getMessage());
		request.setAttribute("exception", exception.getCause());
		request.setAttribute("returnpage", "/right_well.jsp");

	}

	private void handleSaveException(SaveException exception) {
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("info", exception.getMessage());
		request.setAttribute("exception", exception.getCause());
		request.setAttribute("returnpage", "/right_well.jsp");

	}

	private void handleGetException(GetException exception) {
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("info", exception.getMessage());
		request.setAttribute("exception", exception.getCause());
		request.setAttribute("returnpage", "/right_well.jsp");

	}

	private void handleUnexpectedException(Exception exception)
			throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("info", exception.getMessage());
		request.setAttribute("exception", exception.getCause());
		request.setAttribute("returnpage", "/right_well.jsp");

	}
}